/**
	DECLARE @IS_READY_FOR_CONFIRM INT
	EXEC SP_IS_PACKING_SLIP_READY_FOR_CONFIRM 4, @IS_READY_FOR_CONFIRM OUT
	PRINT '@IS_READY_FOR_CONFIRM=' + CONVERT(VARCHAR,@IS_READY_FOR_CONFIRM)
**/
CREATE PROC SP_IS_PACKING_SLIP_READY_FOR_CONFIRM @CUSTOMERSHIPPING_ID INT, @IS_READY_FOR_CONFIRM INT OUT
AS
BEGIN
	SET @IS_READY_FOR_CONFIRM = 1
	DECLARE @CST_AW_BILL_NUMBER VARCHAR(600)
	DECLARE @CST_BOX_COUNT INT
	DECLARE @CST_SHIP_DATE DATETIME
	DECLARE @SHIP_QTY INT
	DECLARE @PULLED_QTY INT

	SELECT @CST_AW_BILL_NUMBER = CST_AW_BILL_NUMBER, @CST_BOX_COUNT = CST_BOX_COUNT, @CST_SHIP_DATE = CST_SHIP_DATE,
				@SHIP_QTY = (SELECT SUM(CSLIT_QTY_SHIPPED) FROM CUSTOMER_SHIPPING_LINE_ITEM_TABLE WHERE CUSTOMERSHIPPING_ID = CST.CUSTOMERSHIPPING_ID),
				@PULLED_QTY = (SELECT SUM(CSLNT_QTY_PULL) FROM Customer_Shipping_Lot_Number_Table WHERE CSLIT_ID IN (SELECT CSLIT_ID FROM CUSTOMER_SHIPPING_LINE_ITEM_TABLE WHERE CUSTOMERSHIPPING_ID = CST.CUSTOMERSHIPPING_ID))
		  FROM CUSTOMER_SHIPPING_TABLE CST WHERE CUSTOMERSHIPPING_ID = @CUSTOMERSHIPPING_ID

		IF ( @CST_AW_BILL_NUMBER IS NOT NULL AND LEN(LTRIM(RTRIM(@CST_AW_BILL_NUMBER))) > 0 
			AND @CST_BOX_COUNT IS NOT NULL AND @CST_BOX_COUNT > 0
			AND @CST_SHIP_DATE IS NOT NULL
			AND ISNULL(@SHIP_QTY,0) = ISNULL(@PULLED_QTY,0)
			)
		BEGIN
			SET @IS_READY_FOR_CONFIRM = 0
		END
	RETURN
END